<?php
include_once(dirname(__FILE__)."/../inc/common.php");
$method = isset($_GET["method"])?$_GET["method"]:"";

$flowClass = new tally_flow();

if($method=="listQry") {
    // 支出
    $dateFrom = isset($_POST["dateFrom"])&&$_POST["dateFrom"]?strtotime($_POST["dateFrom"]):strtotime("1970-01-01");
    $dateTo = isset($_POST["dateTo"])&&$_POST["dateTo"]?strtotime($_POST["dateTo"]):time();
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $outList = $flowClass->getCountByTimeRangeGroupByType($userId,
        tally_flow::$INOUT_EXPENSE, $dateFrom, $dateTo, $tagIds, $categoryId);
    ?>
    <table id="listtb" class="listtable" cellpadding="0" cellspacing="1">
        <tr>
            <th width="1%">
                &nbsp;
            </th>
            <th width="20%">
                类别
            </th>
            <th width="15%">
                金额
            </th>
            <th width="64%"></th>
        </tr>

        <?php
        $sum = 0;
        foreach($outList as $row) {
            $sum += $row["sumOut"];
        ?>
        <TR>
            <TD></TD>
            <TD class="to_left">
                <a href="javascript:fGoto();" onclick="ReportDetail.open(<?php echo $row["flowType"]?>);"><?php echo $row["name"]?></a>
            </TD>
            <TD class="number">
                <?php echo number_format($row["sumOut"], 2, ".", "");?>
            </TD>

            <TD></TD>
        </TR>
        <?php
        }
        ?>

        <TR  id="content">
            <TD></TD>
            <TD  class="to_left">合计</TD>
            <TD class="number"><?php echo number_format($sum, 2, ".", "");?></TD>
            <TD class="number"></TD>
        </TR>


        <tr>
            <td colspan="5"  class="to_left">
                (注：按最新汇率,以人民币计)
            </td>
        </tr>
    </table>

    <?php
}else if($method=="pie") {
    // 支出
    $dateFrom = isset($_POST["dateFrom"])&&$_POST["dateFrom"]?strtotime($_POST["dateFrom"]):strtotime("1970-01-01");
    $dateTo = isset($_POST["dateTo"])&&$_POST["dateTo"]?strtotime($_POST["dateTo"]):time();
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $outList = $flowClass->getCountByTimeRangeGroupByType($userId,
        tally_flow::$INOUT_EXPENSE, $dateFrom, $dateTo, $tagIds, $categoryId);

    foreach ($outList as $k=>$row){
        $outList[$k]["val"] = $row["sumOut"];
    }

    $rs = array(
        "rows" => $outList
    );

    echo json_encode($rs);
}else{
    ?>
    <!-- begin:导航条 -->
    <div class="titlebar">
        <h3>
            支出分类统计
        </h3>
    </div>
    <!-- end:导航条 -->

    <div class="tab_wrapper">
        <div class="body">
            <!-- begin:搜索区 -->
            <form id="schForm" name="schForm" method="post">
                <div class="searchbar" id="searchbar" style="line-height: 150%; display: block;">
                    <div class="base_condition">
                        <table>
                            <tr>
                                <td><SELECT id ="dateSpan" NAME="dateSpan" onChange="updateDate('dateSpan','dateFrom','dateTo');">
                                        <option value="" selected>日期</option>
                                        <option value="today">今日</option>
                                        <option value="theWeek">本周</option>
                                        <option value="theMonth">本月</option>
                                        <option value="theQuarter">本季度</option>
                                        <option value="theYear">今年</option>
                                        <option value="lastWeek">上周</option>
                                        <option value="lastMonth">上月</option>
                                        <option value="lastQuarter">上季度</option>
                                        <option value="lastYear">去年</option>
                                        <option value="last7Day">近7天</option>
                                        <option value="last30Day">近30天</option>
                                        <option value="custom">自定义</option>
                                    </SELECT>
                                    从<INPUT class="Wdate date" TYPE="text" id="dateFrom" NAME="dateFrom" onFocus="WdatePicker();" value="" onChange="updateType('dateSpan');">
                                    至<INPUT class="Wdate date" TYPE="text" id="dateTo" NAME="dateTo" onFocus="WdatePicker();" value="" onChange="updateType('dateSpan');">
                                </td>
                                <td><input id="searchBtn" class="button" type="button" value="查询" onclick="ExpenseReport.search();Report.clearData();Report.searchChart();" /></td>
                            </tr>
                        </table>
                    </div>

                    <TABLE class="complex_condition">
                        <TR>
                            <td>
                                <label for="tags">
                                    标签:
                                </label>
                                <input type="text" class="text rd" id="tags" name="tags"
                                       readonly="readonly" onClick="CM.clearValue(this);">
                                <input type="hidden" value="" name="tagId" id="tagId"/>
                                <a title="选择项目" href="#"
                                   onclick="TagPicker.open(this,'1');return false;"><img src="/images/icons/select.gif" align="absmiddle" />
                                </a>
                            </td>

                            <TD>
                                <label for="categoryName">
                                    支出类别:
                                </label>
                            </td>
                            <td colspan="10">
                                <input type="hidden" id="categoryId" name="categoryId" />
                                <input type="text" class="text rd" id="categoryName" name="categoryName" readonly="readonly" onClick="CM.clearValue(this);">
                                <a title="收支类别" href="#"
                                   onclick="CategoryPicker.open(this,'4');return false;"><img
                                        src="/images/icons/select.gif" align="absmiddle" />
                                </a>
                            </TD>

                            <TD>
                                <INPUT TYPE="radio" NAME="flag" value="1" checked hidden>
                            </TD>

                        </tr>


                    </TABLE>

                </div>
            </form>
            <!-- end:搜索区 -->

            <div class="report_bar">
                <span>数据列表</span>
            </div>

            <div id="dataView" class="data">
                <div align="center">
                    <img align="absmiddle" src='/images/common/loading1.gif'>
                </div>
            </div>


            <div class="report_bar">
                <span>数据图表</span>
            </div>

            <div id="chartView" class="chartView">
                <div align="center">
                    <img align="absmiddle" src='/images/common/loading1.gif'>
                </div>
            </div>

            <!-- end:---------主数据区 -->
        </div>
    </div>
    <?php
}
?>